package feeinterface.protocol.reqn;

import java.util.Date;

import org.apache.log4j.Logger;

import com.ulwx.tool.CTime;
import com.ulwx.tool.DateTime;
import com.ulwx.tool.StringUtils;
import com.ulwx.type.TResult;
import com.ulwx.type.TString;

import feeinterface.dao.MobileDao;
import feeinterface.dao.MobileExtDao;
import feeinterface.modeldb.Mobile;
import feeinterface.modeldb.MobileExt;
import feeinterface.protocol.business.FeeUtil;
import feeinterface.protocol.req.Pro010005;
import feeinterface.protocol.req.ProExchange;
import feeinterface.protocol.res.Res010005;
import feeinterface.protocol.resn.M201001;
import feeinterface.protocol.utils.Constants;
import feeinterface.utils.PhoneUtils;

public class T201001 extends ProExchange {
	private static Logger log = Logger.getLogger(T201001.class);
	public String Protocol;// string(6) 协议编号:：201004
	public Integer Ver;
	public Integer VmVer;// int 见5平台id 3000
	public String IMSI;// string(15) 手机卡串号
	public String SmsCenter;// string 短信中心号码
	public Integer PacketID;// int 打包id。
	public Integer PlatformID;
	public Integer Test;// int 0：真机 1：pc模拟器

	@Override
	public Object genBean() throws Exception {
		// TODO Auto-generated method stub

		TString carrier = new TString("未知");
		int[] areaIDs = this.getAreaID("", IMSI, this.SmsCenter, carrier);
		// 判断手机号码是否注册
		int provinceID = areaIDs[0];
		int cityID = areaIDs[1];

		Date curTime = CTime.getCurrentDateTime();
		// 注册用户
		Mobile mobile = new Mobile();
		mobile.setIMSI(this.IMSI);
		mobile.setRegistCarrier(carrier.getValue());
		mobile.setRegistCityID(cityID);
		mobile.setRegistExtVar("");
		mobile.setRegistFeeMode(0);
		mobile.setRegistGatewayIP(this.getGateWayIP());
		mobile.setRegistGatewayKW(this.getGateWayKey());
		mobile.setRegistCountryID(Constants.Area.China);// 暂定为中国
		mobile.setRegistLibVer(this.Ver);
		mobile.setRegistMobile("");
		mobile.setRegistPacketID("");
		mobile.setRegistPlatform(PlatformID + "");
		mobile.setRegistProtocol(this.Protocol);
		mobile.setRegistProvinceID(provinceID);
		mobile.setRegistServiceCoID(this.PacketID);
		mobile.setRegistSmsCenter(PhoneUtils.getAdjustedPhone(this.SmsCenter));
		mobile.setRegistTime(curTime);
		mobile.setRegistVmVer(this.VmVer);

		MobileExt met = new MobileExt();

		met.setIMSI(this.IMSI);
		met.setLastCityID(cityID);
		met.setLastExtvar("");
		met.setLastFeeMode(0);
		met.setLastRequestFeeTime(curTime);
		met.setLastGatewayIP(this.getGateWayIP());
		met.setLastGatewayKW(this.getGateWayKey());
		met.setLastLibVer(Ver);
		met.setLastPacketID("");
		met.setLastPlatform(PlatformID + "");
		met.setLastProvinceID(provinceID);
		met.setLastServiceCoID(this.PacketID);
		met.setLastVmVer(VmVer);

		try {
			TResult<Mobile> oldMoile = new TResult<Mobile>();
			boolean IsOldUser = MobileDao.registIfnew(mobile,oldMoile);
			if (IsOldUser) {// 老用户
				MobileExt meFromDb = MobileExtDao.getByIMSI(IMSI);
				met.setCurDayFee(meFromDb.getCurDayFee());
				met.setLastUseChannelTime(meFromDb.getLastUseChannelTime());
				met.setCurDayUseChannelCnt(meFromDb.getCurDayUseChannelCnt());
				met.setCurMonthFee(meFromDb.getCurMonthFee());
				met.setCurMonthUseChannelCnt(meFromDb
						.getCurMonthUseChannelCnt());
				met.setCurMonthUseChannelDayCnt(meFromDb
						.getCurMonthUseChannelDayCnt());
				met.setDayGetPhoneCnt(meFromDb.getDayGetPhoneCnt());
				met.setDoFee(meFromDb.getDoFee());

				met.setMonthGetPhoneCnt(meFromDb.getMonthGetPhoneCnt());
				met.setDayUpdateLib(meFromDb.getDayUpdateLib());
			} else {// 如果是新用户
				met.setCurDayFee(0);
				met.setCurDayUseChannelCnt(0);
				met.setCurMonthFee(0);
				met.setCurMonthUseChannelCnt(0);
				met.setCurMonthUseChannelDayCnt(0);
				met.setDayGetPhoneCnt(0);
				met.setDoFee(Constants.DoFee.NotUseChannel);

				met.setMonthGetPhoneCnt(0);
				met.setDayUpdateLib(0);
				MobileExtDao.InsertOrUpdate(met);

			}
		} catch (Exception e) {
			log.error("", e);
		}

		// 返回获取手机号码信息
		Pro010005 p010005 = new Pro010005();

		p010005.setRequest(this.getRequest());

		p010005.Carrier = 0;
		p010005.COID = this.PacketID;
		p010005.RequestFrom = (int)Constants.RequestFrom.CLIENT;
		p010005.IMSI = this.IMSI;

		p010005.PacketID = this.PacketID+"";
		p010005.Platform = PlatformID + "";
		p010005.Protocol = "010005";

		p010005.SmsCenter = this.SmsCenter;
		p010005.Ver = this.Ver;
		p010005.VmVer = this.VmVer;
		Res010005 res = (Res010005) p010005.genBean();

		M201001 m = new M201001();

		m.GetMobilePageUrl = res.GetMobilePageUrl;
		m.GetMobileStep = res.GetMobileStep;
		m.Message = res.Message;
		m.Mobile = res.Mobile;
		m.SMSCatPort = res.SMSCatPort;
		m.SMSContent = res.SMSContent;
		m.Status = res.Status;
		DateTime now = new DateTime();


		m.ActiveFeeTime = MonthFeeRequestDate.getToday().substring(5);

		try {
			if(StringUtils.isEmpty(m.Mobile)){
				MobileExtDao.updateGetPhoneCnt(this.IMSI);
			}
		} catch (Exception e) {
			log.error("", e);
		}

		return m;

	}

	@Override
	public String validate() throws Exception {
		// TODO Auto-generated method stub
		this.SmsCenter=PhoneUtils.getAdjustedPhone(this.SmsCenter);
		
		return null;
	}

}
